package smartlifev30.com.sdk.api.gateway_app;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import smartlifev30.com.sdk.util.JsonUtil;
import smartlifev30.com.sdk.api.CallBack;
import smartlifev30.com.sdk.api.ConstantGateway;
import smartlifev30.com.sdk.bean.request.DelRoomRequest;
import smartlifev30.com.sdk.bean.request.EditRoomRequest;
import smartlifev30.com.sdk.bean.request.Room;
import smartlifev30.com.sdk.bean.request.RoomAddRequest;
import smartlifev30.com.sdk.bean.request.RoomQueryRequest;
import smartlifev30.com.sdk.bean.response.LoginResponse;
import smartlifev30.com.sdk.bean.response.RoomQueryResponse;
import smartlifev30.com.sdk.net.baiwei.NetManager;
import smartlifev30.com.sdk.net.baiwei.ResponseHelper;

/**
 * Created by tys on 2018/8/13.
 */

public class RoomMgmtGatewayImpl implements IRoomMgmtGateway {

    @Override
    public void roomQuery(@NonNull String userName, @NonNull String token, @NonNull String sn,
                          @NonNull CallBack<RoomQueryResponse> callBack) {
        RoomQueryRequest roomQueryRequest = new RoomQueryRequest();
        roomQueryRequest.setFrom(userName);
        roomQueryRequest.setTo(sn);
        roomQueryRequest.setMsg_id(callBack.getMsgId());
        roomQueryRequest.setMsg_class(ConstantGateway.MSG_CLASS_ROOM);
        roomQueryRequest.setMsg_name("room_query");
        roomQueryRequest.setMsg_type(ConstantGateway.MSG_TYPE_GET);
        roomQueryRequest.setToken(token);
        NetManager.getInstance().sendMessage(JsonUtil.toString(roomQueryRequest));
        ResponseHelper.addListen(callBack);
    }

    @Override
    public void roomAdd(@NonNull String userName, @NonNull String token, @NonNull String sn,
                        @NonNull Room room, @NonNull CallBack<LoginResponse> callBack) {
        RoomAddRequest roomAddRequest = new RoomAddRequest();
        roomAddRequest.setFrom(userName);
        roomAddRequest.setTo(sn);
        roomAddRequest.setMsg_id(callBack.getMsgId());
        roomAddRequest.setMsg_class(ConstantGateway.MSG_CLASS_ROOM);
        roomAddRequest.setMsg_name("room_add");
        roomAddRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        roomAddRequest.setToken(token);
        roomAddRequest.setRoom(room);
        NetManager.getInstance().sendMessage(JsonUtil.toString(roomAddRequest));
        ResponseHelper.addListen(callBack);
    }

    @Override
    public void delRoom(@NonNull String userName, @NonNull String token, @NonNull String sn, @Nullable List<Integer> room, @NonNull CallBack<LoginResponse> callBack) {
        DelRoomRequest delRoomRequest = new DelRoomRequest();
        delRoomRequest.setFrom(userName);
        delRoomRequest.setTo(sn);
        delRoomRequest.setMsg_id(callBack.getMsgId());
        delRoomRequest.setMsg_class(ConstantGateway.MSG_CLASS_ROOM);
        delRoomRequest.setMsg_name("room_del");
        delRoomRequest.setToken(token);
        List<DelRoomRequest.RoomList> list = null;
        if (room != null) {
            list = new ArrayList<>();
            for (Integer id : room) {
                list.add(delRoomRequest.new RoomList(id));
            }
        }
        delRoomRequest.setRoom_list(list);
        NetManager.getInstance().sendMessage(JsonUtil.toString(delRoomRequest));
        ResponseHelper.addListen(callBack);
    }

    @Override
    public void editRoom(@NonNull String userName, @NonNull String token, @NonNull String sn, @Nullable Room room, @NonNull CallBack<LoginResponse> callBack) {
        EditRoomRequest editRoomRequest = new EditRoomRequest();
        editRoomRequest.setFrom(userName);
        editRoomRequest.setTo(sn);
        editRoomRequest.setMsg_id(callBack.getMsgId());
        editRoomRequest.setMsg_class(ConstantGateway.MSG_CLASS_ROOM);
        editRoomRequest.setMsg_name("room_edit");
        editRoomRequest.setMsg_type(ConstantGateway.MSG_TYPE_GET);
        editRoomRequest.setRoom(room);
        NetManager.getInstance().sendMessage(JsonUtil.toString(editRoomRequest));
        ResponseHelper.addListen(callBack);
    }



}
